import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-08-12
 * Time: 21:45
 */
// 这个 Servlet 用来动态的生成主页面.
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if (session == null) {
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("当前用户未登录！");
            return;
        }
        String userName = (String)session.getAttribute("username");
        System.out.println(userName);
        if (userName == null) {
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("当前用户未登录！");
            return;
        }

        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write("欢迎你！"+userName);
    }
}
